package Amazon::Bezos::Account;
use strict;
use warnings;

use Object::Tiny qw(name access_key_id secret_access_key account_number);
use Class::ErrorHandler;

sub new {
    my $class = shift;
    my %args  = @_;
    $args{access_key_id}     ||= $ENV{'AWS_ACCESS_KEY_ID'};
    $args{secret_access_key} ||= $ENV{'AWS_ACCESS_KEY_SECRET'};
    return bless \%args, $class;
}

sub load {    # need default account option logic.
    my ($class, $name, $file) = @_;
    unless (defined $file) {
        require File::HomeDir;
        require File::Spec;
        $file = File::Spec->catfile(File::HomeDir->my_home, 'bezos.yaml');
        return $class->error("$file could not be found.") unless -e $file;
    }
    require YAML::Tiny;
    my $accounts = YAML::Tiny->read($file);
    my ($acct) = grep { $_->{name} eq $name } @$accounts;
    my %args;
    if ($acct) {
        %args = (
                 name => $acct->{name},
                 access_key_id     => $acct->{access_key_id},
                 secret_access_key => $acct->{secret_access_key},
                 account_number     => $acct->{account_number},
        );
    }    # error
    return Amazon::Bezos::Account->new(%args);
}

1;

__END__

=head1 NAME

Amazon::Bezos::Account - Object representation an Amazon Web
Services account.

=head1 SYNOPSIS

  #!/usr/bin/perl
  use warnings;
  use strict;

  use Amazon::Bezos::Account;
  
  # create account using env variables. see TESTING.
  my $bezos = Amazon::Bezos::Amazon->new; 

  # explicitly set account information
  $bezos = Amazon::Bezos::Amazon->new; 

  # load Test account from bezos .yaml coniguration file
  $bezos = Amazon::Bezos::Amazon->load('Test','/path/to/bezos.yaml'); 

=head1 DESCRIPTION

Amazon::Bezos::Account - Object representation an Amazon Web
Services account.

=head1 METHODS

=head2 Amazon::Bezos::Account->new([access_key_id => $key, secret_access_key => $secret, account_number => $num]);

Instantiates an account object with the passed credentials.
If an I<access_key_id> or I<secret_access_key> param is not
passed the method will try and use the AWS_ACCESS_KEY_ID and
AWS_ACCESS_KEY_SECRET environmental variables respectively.

Returns a account object if successful. This method is
typically called the L<Amazon::Bezos> constructor.

=head2 Amazon::Bezos->load($name[,$file]);

This class method works like new, but it loads its account
information from a YAML file according to a user defined
name. If a file name is not passed the system will look in
the user's home directory for a file called I<bezos.yaml>.

=head2 access_key_id

=head2 secret_access_key

=head2 name

=head2 account_number

=head2 error

Used to set the message for the last error the account
encountered.

=head2 errstr

Returns a human readable error string for the last error the
account encountered.

=head1 AUTHOR & COPYRIGHT

Please see the L<Amazon::Bezos> manpage for author, copyright, and
license information.

